/**
 * 时间戳
 * @param {*} timestamp  时间戳
 */
const timestampToTime = (timestamp) => {
    let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D =
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
    let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s
};

const timestampToDate = (timestamp) => {
  let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear()
  let M =
    (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  let D =
    (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
  return Y + '/' + M + '/' + D
};

const timestampToMonth = (timestamp) => {
  let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear()
  let M =
    (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  return Y + '/' + M
};

/**
 * 存储localStorage
 */
const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
      content = JSON.stringify(content);
    }
    // if(!expires || isNaN(expires)){
    //   expires = 60;
    // }
    // let params = {
    //   value: content,
    //   time: (new Date().getTime())+ expires*1000*60
    // }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
    if (!name) return;
    let content = window.localStorage.getItem(name)
    try {
      content = JSON.parse(content)
    }catch (e) {
      content = content;
    }
    return content;

    // if(!localStorage){return false;}
    // var cacheVal = window.localStorage.getItem(name);
    // var result = result = JSON.parse(cacheVal);
    // var now = new Date().getTime();
    // if(!result){return null;}//缓存不存在
    // if(now>result.time){//缓存过期
    //   this.removeStore(name);
    //   return null;
    // }
    // try{
    //   return JSON.parse(result.value);
    // }catch(e){
    //   return result.value;
    // }
}

/**
 * 删除localStorage
 */
const removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}

/**
 * 删除localStorage
 */
const clearStore = () => {
  if(!window.localStorage){return false;}
  window.localStorage.clear();
}

/**
 * 设置cookie
 **/
function setCookie(name, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
};

/**
 * 获取cookie
 **/
function getCookie(name) {
    let reg = RegExp(name + '=([^;]+)');
    let arr = document.cookie.match(reg);
    if (arr) {
        return arr[1];
    } else {
        return '';
    }
};

/**
 * 删除cookie
 **/
function delCookie(name) {
    setCookie(name, null, -1);
};

/**
 * 导出
 **/
export {
    timestampToTime,
    timestampToDate,
    timestampToMonth,
    setStore,
    getStore,
    removeStore,
    clearStore,
    setCookie,
    getCookie,
    delCookie
}
